package com.wefans.wefansapp.common.tool.utils;


// java
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import com.wefans.wefansapp.common.tool.debug.MFDebug;


/**
 * 시간 관련 함수를 모아둔 Utility Class
 * 
 * @author changhyun
 *
 */
public class MFUtilTime
{
    /**************************************************************************************************************
     * final property
     **************************************************************************************************************/
    /* Default Date Format */
    public static final String DATE_FORMAT = "yyyyMMdd";

    /**************************************************************************************************************
     * current time
     **************************************************************************************************************/

    /**
     * 현재 날짜를 가져오는 함수. (yyyyMMdd의 형태로 반환한다.)
     * 
     * @return
     */
    public static String getCurrentDate() 
    {
        MFDebug.d(">> STUtilTime::getCurrentDate()");
        
        return changeTimeFormat(System.currentTimeMillis(), DATE_FORMAT);
    }

    /**
     * 현재의 날짜를 반환한다.
     * 
     * @param dateFormat : 반환받을 String Format
     * @return
     */
    public static String getCurrentDate(String dateFormat) 
    {
        MFDebug.d(">> STUtilTime::getCurrentDate(dateFormat)");
        MFDebug.d("++ dateFormat=" + dateFormat);
        
        return changeTimeFormat(System.currentTimeMillis(), dateFormat);
    }
    
    /**
     * 해당 Format에 맞게 날짜를 변환한다.
     * 
     * @param lTime: 변환하길 원하는 시간
     * @param dateFormat : 반환받을 String Format
     * @return
     */
    public static String changeTimeFormat(long lTime, String dateFormat) 
    {
        MFDebug.d(">> STUtilTime::changeTimeFormat(lTime, dateFormat)");
        
        return changeTimeFormat(lTime, dateFormat, new Locale("ko", "KOREA"));
    }
    
    /**
     * 해당 Format과 Locale 에 맞게 날짜를 변환한다. 
     * @param lTime
     * @param dateFormat
     * @param locale
     * @return
     */
    public static String changeTimeFormat(long lTime, String dateFormat, Locale locale)
    {
        MFDebug.d(">> STUtilTime::changeTimeFormat(lTime, dateFormat, locale)");
        MFDebug.d("++ dateFormat=" + dateFormat);
        
        SimpleDateFormat sdf = new SimpleDateFormat( dateFormat, locale );
        Date date = new Date( lTime );
        return sdf.format( date.getTime() );
    }
    
    
    
    /**
     * 입력받은 날짜를 밀리세컨드로 바꿔서 스트링형태로 리턴
     * @param strUserHourMinute ex)5시6분 => 0506 
     * @return
     */
    public static String getMilliSecond(String strUserHourMinute){
        MFDebug.d(">> STUtilTime::getMilliSecond(strUserHourMinute)");
        MFDebug.d("++ strUserHourMinute=" + strUserHourMinute);
        
        String strMill = "";
        
        Calendar calToday = Calendar.getInstance();
        int hourOfDay = calToday.get(Calendar.HOUR_OF_DAY);
        int minute = calToday.get(Calendar.MINUTE);
        
        int nUserHM = Integer.parseInt(strUserHourMinute);
        int nNowHM = Integer.parseInt(hourOfDay+""+minute);
        
        
        if(nUserHM>nNowHM){
            //오늘날짜에 알람세팅!
            calToday.set(Calendar.HOUR_OF_DAY, Integer.parseInt(strUserHourMinute.substring(0, 2)));
            calToday.set(Calendar.MINUTE, Integer.parseInt(strUserHourMinute.substring(2)));
            calToday.set(Calendar.SECOND, 0);
            calToday.set(Calendar.MILLISECOND, 0);
            
        }else{
            //내일날짜에 알람세팅!
            calToday.add(Calendar.DATE, 1);
            calToday.set(Calendar.HOUR_OF_DAY, Integer.parseInt(strUserHourMinute.substring(0, 2)));
            calToday.set(Calendar.MINUTE, Integer.parseInt(strUserHourMinute.substring(2)));
            calToday.set(Calendar.SECOND, 0);
            calToday.set(Calendar.MILLISECOND, 0);
        }
        MFDebug.d("hour : " + strUserHourMinute.substring(0, 2));
        MFDebug.d("minute : " + strUserHourMinute.substring(2));
        strMill = calToday.getTimeInMillis()+"";
        
        
        return strMill;
    }
    
    /**
     * 현재시간과 param으로 넘겨준 시간을 비교하여 차이난 날을 반환한다.
     * 
     * @param strYear "2012"
     * @param strMonth "12"
     * @param strDay "31"
     * @return int 차이난 날(day)
     */
    public static int compareCurrentDate(String strYear, String strMonth, String strDay)
    {
        MFDebug.d(">> STUtilTime::compareCurrentDate()");
        Date dateCurrent = new Date( System.currentTimeMillis() );
        Date dateUserSet = new Date( Integer.parseInt(strYear)-1900, Integer.parseInt(strMonth)-1, Integer.parseInt(strDay));
        
        int nDifferday = (int) ((dateCurrent.getTime() - dateUserSet.getTime()) / (1000*60*60*24));
        MFDebug.d("++ nDifferday=" + nDifferday);
        return nDifferday;        
    }
    
    
    
}